home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / ThreadStuff / Failure.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-28  |  3.7 KB  |  128 lines  |  [TEXT/KAHL]

  1. /*
  2.     File:        Failure.h
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13. #ifndef __FAILUREHANDLER__
  14. #define __FAILUREHANDLER__
  15.  
  16. #ifndef __RESOURCES__
  17. #include <Resources.h>
  18. #endif
  19.  
  20. #ifndef __MEMORY__
  21. #include <Memory.h>
  22. #endif
  23.  
  24. #include <setjmp.h>
  25.  
  26. #define ePointerNil            -110
  27. #define eGeneralErr            -1
  28. #define eAssertionFailure    -30000
  29.  
  30. #define kMagicFailID        'fail'
  31.  
  32. #define kInExceptionHandler    0x001
  33.  
  34. /*
  35. // Data types:
  36. */
  37. struct FailStack
  38. {
  39.     struct FailStack*        next;
  40.     OSErr                    error;
  41.     jmp_buf                    env;
  42.     long                    magicFailID;
  43.     long                    flags;
  44. };
  45.  
  46. typedef struct FailStack    FailStack;
  47.  
  48. typedef void (*NotifyFailureProc)(void);
  49.  
  50. /*
  51. // Prototypes
  52. */
  53. void                        _SetupTry(FailStack* failInfo);
  54. void                        _PopFailureStack(void);
  55. void                        _Failure( OSErr theErr );
  56. void                        _FailOSErr( OSErr theErr );
  57. OSErr                        _TheFailError(void);
  58. void                        _FailAgain(void);
  59.  
  60. void                        SetExceptionNotifyProc(NotifyFailureProc notifyProc);
  61. Boolean                        ExceptionNotifyProcInstalled(void);
  62.  
  63. void * GetExceptionChain ();
  64. void SetExceptionChain ( void * );
  65.  
  66. extern const char* GetErrorName ( OSErr error );
  67.  
  68. /***********************************|****************************************/
  69.  
  70. /*
  71. //    TRY / EXCEPT / ENDTRY macros
  72. */
  73.     #undef TRY
  74.     #undef EXCEPTION
  75.     #undef ENDEXCEPTION
  76.     #undef NOHANDLER
  77.     #undef CHECKPOINT
  78.     #undef RETRY
  79.     #undef FAIL
  80.     #undef PASSEXCEPTION
  81.     #undef FAILOSErr
  82.     #undef FAILNULL
  83.     #undef FAILifTRUE
  84.     #undef FAILifFALSE
  85.     
  86.     Boolean BreakOnFailures();
  87.     
  88.     # define    TRY        { do { FailStack failInfo; _SetupTry(&failInfo); if( setjmp(failInfo.env) == 0 ) {
  89.     
  90.     # define    EXCEPTION        } else { keith<<"EXCEPTN : #"<<_TheFailError()<<" (" << GetErrorName(_TheFailError()) << ") file "<<__FILE__<<", line "<<__LINE__<<endl; 
  91.  
  92.     # define    ENDEXCEPTION    }; _PopFailureStack(); } while(false); }
  93.  
  94.     # define    NOHANDLER        }; _PopFailureStack(); } while(false); }
  95.     
  96.     # define    CHECKPOINT()    { }
  97.  
  98.     # define    FAIL(code)        { keith << "FAIL    : #"<< code <<" (" << GetErrorName ( code ) << ") file "<<__FILE__<<", line "<<__LINE__<<endl; if (BreakOnFailures()) BreakStr ("\pFAILURE");; _FailOSErr(code); }
  99.  
  100.     # define    PASSEXCEPTION()    { keith<<"PASSEXCP: #"<<EXCEPTIONCODE<<" (" << GetErrorName (EXCEPTIONCODE) << "), file "<<__FILE__<<", line "<<__LINE__<<endl; _FailAgain(); }
  101.  
  102.     # define    FAILOSErr(code) { short _FAILCODE=code; if ( _FAILCODE ) { keith << "FAIL/OS : #"<<_FAILCODE<<" (" << GetErrorName ( code ) << "), file "<<__FILE__<<", line "<<__LINE__<<endl; if (BreakOnFailures()) BreakStr ("\pFAILURE");_FailOSErr(_FAILCODE); } }
  103.  
  104.     # define    FAILNULL(p)     { if (!p) { keith<<"FAILNULL: file "<<__FILE__<<", line "<<__LINE__<<endl; if (BreakOnFailures()) BreakStr ("\pFAILNULL");  _FailOSErr(-108); } }
  105.     # define    FAILifTRUE(p)     { if (p)  { keith<<"FAILTRUE: file "<<__FILE__<<", line "<<__LINE__<<endl; if (BreakOnFailures()) BreakStr ("\pFAILTRUE"); _FailOSErr(-1);} }
  106.     # define    FAILifFALSE(p)     { if (!p) { keith<<"FAILFALS: file "<<__FILE__<<", line "<<__LINE__<<endl; if (BreakOnFailures()) BreakStr ("\pFAILFALSE"); _FailOSErr(-1);} }
  107.  
  108.     # define    FAILIf(cond,error)    if (cond) FAIL(error);else
  109.  
  110. /*
  111. //    Convenience macros
  112. */
  113. #define FAILResError()                _FailOSErr( ResError() )
  114. #define FAILMemError()                _FailOSErr( MemError() )
  115.  
  116. #define FAILResErrorOrNil(ptr)        do { FailResError(); FAILNil(ptr); } while(false)
  117. #define FAILMemErrorOrNil(ptr)        do { FailMemError(); FailNil(ptr); } while(false)
  118.  
  119. /*
  120. // Assertion macros
  121. */
  122. #define Assert(truth)                if( !(truth) ) FailOSErr( eAssertionFailure )
  123. #define AssertPtrValid(p)            if( (p == nil) || (((long)(p) & 1) != 0) ) FailOSErr( eAssertionFailure )
  124. #define AssertHandleValid(h)        AssertPtrValid(h); AssertPtrValid(*h)
  125.  
  126. #define EXCEPTIONCODE    _TheFailError()
  127. #endif
  128.